//
//  GYLoginView.m
//  Gaia
//
//  Created by weixb on 16/9/14.
//  Copyright © 2016年 weixb. All rights reserved.
//

#import "YQInputMobileView.h"
#import "AppDelegate.h"
#import <YQTools.h>
#import <ReactiveCocoa.h>
#import <Masonry.h>
#define KBLUECOLOR_LIGHT_3D82E5 [UIColor yq_colorWithHexString:@"#3D82E5"]

@interface YQInputMobileView ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *loginView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *nextBtn;
@property (nonatomic, strong) UIButton *closeBtn;

@end

@implementation YQInputMobileView

{
    CGFloat cellWidth;
    CGFloat cellHeight;
    UIView *_maskView;
}

#pragma mark - initialize
- (void)layoutSubviews {
    [super layoutSubviews];
    cellWidth = self.bounds.size.width;
    cellHeight = self.bounds.size.height;
    [self configConstraints];
}
#pragma mark - public
- (void)showInView:(UIView *)view {
    [view addSubview:self];
    if (_maskView == nil) {
        _maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _maskView.backgroundColor = [UIColor blackColor];
        _maskView.alpha = 0;
    }
    [view addSubview:_maskView];
    [view addSubview:self.loginView];
    [_loginView addSubview:self.titleLabel];
    [_loginView addSubview:self.phoneTextField];
    [_loginView addSubview:self.nextBtn];
    [_loginView addSubview:self.closeBtn];
    [self layoutSubviews];
    [UIView animateWithDuration:0.4 animations:^{
        _maskView.alpha = 0.3;
        _loginView.alpha = 1;
    }];
    [self configSubViewState];
}
#pragma mark - RAC设置状态
- (void)configSubViewState{
    //检测是否输入的是手机号码
    RACSignal *validMobileSignal = [_phoneTextField.rac_textSignal map:^id(NSString *value) {
        
        return @(value.length == 11);
    }];
    [validMobileSignal subscribeNext:^(NSNumber *isActive) {
        _nextBtn.enabled = isActive.boolValue;
        _nextBtn.backgroundColor = isActive.boolValue ? KBLUECOLOR_LIGHT_3D82E5 : [UIColor lightGrayColor];
    }];
}
#pragma mark - <UITextFieldDelegate>
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
    if ([string isEqualToString:@"\n"])
    {
        return YES;
    }
    // 输入长度限制
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (_phoneTextField == textField)
    {
        if ([toBeString length] > 11) {
            textField.text = [toBeString substringToIndex:11];
            return NO;
        }
    }
    return YES;
}
//- (void)textFieldDidEndEditing:(UITextField *)textField {
//    if (textField.text.length != 11) {
//        return;
//    }
//    [self clickNextBtn:_nextBtn];
//}

#pragma mark - target
- (void)clickNextBtn:(UIButton *)sender {
    NSLog(@"点击了下一步");
    [[NSUserDefaults standardUserDefaults] setObject:_phoneTextField.text forKey:@"defaultMobile.login"];
    [_phoneTextField resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(nextStepWithphoneNumber:)]) {
        [self.delegate nextStepWithphoneNumber:_phoneTextField.text];
    }
}
- (void)clickCloseBtn:(UIButton *)sender {
    NSLog(@"点击了关闭");
//    exit(0);
    if ([self.delegate respondsToSelector:@selector(dismiss)]) {
        [self.delegate dismiss];
    }
}

#pragma mark - private

- (void)configConstraints {
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(18);
        make.centerX.equalTo(_loginView);
    }];
    [_phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_loginView).offset(-10*YQHorizentalMutiplier());
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(40*YQHorizentalMutiplier());
    }];
    [_nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_loginView.mas_bottom).offset(-20);
        make.centerX.equalTo(_loginView);
        make.width.height.equalTo(_phoneTextField);
    }];
    [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel);
        make.right.mas_equalTo(-15);
    }];
}
#pragma mark - getter

- (UIView *)loginView {
	if (_loginView == nil) {
        _loginView = [[UIView alloc] init];
        _loginView.bounds = CGRectMake(0, 0, SCREEN_WIDTH * .9f, 200 * YQHorizentalMutiplier());
        _loginView.center = CGPointMake(SCREEN_WIDTH/2, SCREENH_HEIGHT/2);
        _loginView.backgroundColor = [UIColor whiteColor];
        _loginView.layer.cornerRadius = 5;
        _loginView.layer.masksToBounds = YES;
        _loginView.alpha = 0.5;
	}
	return _loginView;
}

- (UILabel *)titleLabel {
	if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = @"登录";
	}
	return _titleLabel;
}
- (UITextField *)phoneTextField {
	if (_phoneTextField == nil) {
        _phoneTextField = [[UITextField alloc] init];
        _phoneTextField.placeholder = @"手机号";
        _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        _phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _phoneTextField.delegate = self;
        _phoneTextField.layer.cornerRadius = 2;
        _phoneTextField.layer.borderWidth = .5f;
        _phoneTextField.layer.borderColor = [UIColor yq_colorWithHexString:@"999999"].CGColor;
        _phoneTextField.layer.masksToBounds = YES;
        CGFloat width = 30;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, width)];
        imageView.contentMode = UIViewContentModeRight;
        imageView.image = [UIImage imageNamed:@"icon_user_mobile"];
        _phoneTextField.leftView = imageView;
        _phoneTextField.leftViewMode = UITextFieldViewModeAlways;
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultMobile.login"] length]) {
            _phoneTextField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"defaultMobile.login"];
        }
	}
	return _phoneTextField;
}
- (UIButton *)nextBtn {
	if (_nextBtn == nil) {
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        _nextBtn.backgroundColor = KBLUECOLOR_LIGHT_3D82E5;
        _nextBtn.layer.cornerRadius = 5;
        _nextBtn.layer.masksToBounds = YES;
        [_nextBtn addTarget:self action:@selector(clickNextBtn:) forControlEvents:UIControlEventTouchUpInside];
	}
	return _nextBtn;
}
- (UIButton *)closeBtn {
	if (_closeBtn == nil) {
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
        [_closeBtn setImage:[UIImage imageNamed:@"icon_close"] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(clickCloseBtn:) forControlEvents:UIControlEventTouchUpInside];
	}
	return _closeBtn;
}

@end
